package com.holi.hiteacher.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.holi.hiteacher.R;
import com.holi.hiteacher.bean.EmailBean;
import com.holi.hiteacher.data.DatabaseUtil;
import com.holi.hiteacher.databinding.ItemEmailListBinding;
import com.holi.hiteacher.utils.ToastMaster;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EmailListAty extends BaseActivity {
    public static void start(Context context) {
        Intent intent = new Intent(context, EmailListAty.class);
        context.startActivity(intent);
    }

    @BindView(R.id.email_list)
    ListView mEmailList;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    private ArrayList<EmailBean> mDatas = new ArrayList<>();
    public static boolean isForeground = false;
    private EmailReceiver mEmailReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_email_list);
        ButterKnife.bind(this);

        initToolbar();

        MyAdapter adapter = new MyAdapter(this, R.layout.item_email_list, mDatas);
        mEmailList.setAdapter(adapter);

        mEmailList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EmailAty.start(EmailListAty.this, mDatas.get(position).getId());
            }
        });

        registerEmailReceiver();
    }

    private void updateData() {
        mDatas.clear();
        ArrayList<EmailBean> data = DatabaseUtil.queryAllEmailBean();
        for (EmailBean bean : data) {
            mDatas.add(0, bean);
        }
        ((MyAdapter) mEmailList.getAdapter()).notifyDataSetChanged();
    }

    public class MyAdapter extends ArrayAdapter<EmailBean> {

        private LayoutInflater mInflater;
        private int mResource;
        private ItemEmailListBinding binding;

        public MyAdapter(Context context, int resource, List<EmailBean> objects) {
            super(context, resource, objects);
            mInflater = LayoutInflater.from(context);
            mResource = resource;
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                binding = DataBindingUtil.inflate(mInflater, mResource, parent, false);
                convertView = binding.getRoot();
                convertView.setTag(binding);
            } else {
                binding = (ItemEmailListBinding) convertView.getTag();
            }
            binding.setItemData(getItem(position));
            return convertView;
        }

    }

    public static final String EMAIL_RECEIVED_ACTION = "com.holi.hiteacher.EMAIL_RECEIVED_ACTION";

    public class EmailReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (EMAIL_RECEIVED_ACTION.equals(intent.getAction())) {
                updateData();
                ToastMaster.showToast(EmailListAty.this, "数据已更新");
            }
        }
    }

    public void registerEmailReceiver() {
        mEmailReceiver = new EmailReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(EMAIL_RECEIVED_ACTION);
        registerReceiver(mEmailReceiver, filter);
    }

    private void initToolbar() {
        mToolbar.setTitle("我的消息");
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateData();
        isForeground = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isForeground = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mEmailReceiver);
    }
}
